<include file="Public/min-header"/>
<div class="wrapper">
  <include file="Public/breadcrumb"/>
  <link href="/Public/plugins/daterangepicker/daterangepicker-bs3.css" rel="stylesheet" type="text/css" />
  <script src="/Public/plugins/daterangepicker/moment.min.js" type="text/javascript"></script>
  <script src="/Public/plugins/daterangepicker/daterangepicker.js" type="text/javascript"></script>
    <section class="content"> 
        <!-- Main content -->
		<style type="text/css">
			.autopush{background: #eee;border:1px solid #ccc;padding:0;width:250px;display:none;}
			.autopush li{cursor: pointer;list-style:none;border-bottom: 1px dashed #ccc;padding:5px;}
			.driver_autopush{background: #eee;border:1px solid #ccc;padding:0;width:250px;display:none;}
			.driver_autopush li{cursor: pointer;list-style:none;border-bottom: 1px dashed #ccc;padding:5px;}
		</style>
        <div class="container-fluid">
            <div class="pull-right">
				<button class="btn btn-info" style="background: #68adbf;" type="button" onclick="location.href='/index.php/Admin/Order/tuihuo_list'">
				    <i class="glyphicon glyphicon-th-list"></i>
				  		 返回列表
				</button>
                <a href="javascript:history.go(-1)" data-toggle="tooltip" title="" class="btn btn-default" data-original-title="返回"><i class="fa fa-reply"></i></a>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="fa fa-list"></i>退货订单信息</h3>
                </div>
                <div class="panel-body">
                    <!--表单数据-->
                    <form method="post" action="/index.php?m=admin&c=Order&a=tuihuo_edit_order&id={:I('id')}" id="order-add">
                        <div class="tab-pane">
                            <table class="table table-bordered">
                                <tbody>
<!-- 								<tr>
								    <td>单号/备注:</td>
								    <td>
								    <div class="form-group ">
								        <div class="col-xs-2">
								            <input name="order_sn" id="order_sn" value="{$order.order_sn}" class="form-control" placeholder="订单号或者备注信息" style="width:520px;" autocomplete="off"/>
								        </div>
								        <div class="col-xs-2">
										    <span id="err_consignee" style="color:#F00; display:none;">收货人名字或店铺不能为空</span>
								        </div> 
								    </div>
								    </td>
								</tr> -->
                                <tr>
                                    <td>订单号:</td>
                                    <td>
                                    <div class="form-group ">
	                                    <div class="col-xs-2">
	                                        <input name="order_sn" id="order_sn" value="{$order.order_sn}" class="form-control" placeholder="原订单号" style="width: 250px;" oninput="autopush(this,'user')" autocomplete="off"/>
											<input type="hidden" name="order_id" id="order_id" value="{$order.order_id}" />
											<ul id="orders" class="autopush"></ul>
                                        </div><span style="color: red;">（取消、作废单不能退货。）</span>
                                        <div class="col-xs-2">
										    <span id="err_consignee" style="color:#F00; display:none;">收货人名字或店铺不能为空</span>
                                        </div> 
                                    </div>    
                                    </td>
                                </tr>
                                <tr>
                                    <td>收货人/客店:</td>
                                    <td>
                                    <div class="form-group ">
	                                    <div class="col-xs-2">
	                                        <input name="consignee" id="consignee" value="{$order.nickname}" class="form-control" placeholder="收货人名字或店铺" style="width: 250px;" readonly="readonly" autocomplete="off"/>
											<input type="hidden" name="user_id" id="user_id" value="{$order.user_id}" />
                                        </div>  
                                        <div class="col-xs-2">
										    <span id="err_consignee" style="color:#F00; display:none;">收货人名字或店铺不能为空</span>
                                        </div> 
                                    </div>    
                                    </td>
                                </tr>
								<tr>
								    <td>商家店铺:</td>
								    <td>
								    <div class="form-group ">
								        <div class="col-xs-2">
								            <input name="store_name" id="store_name" value="{$order.store_name}" class="form-control" placeholder="商家的商铺名"  style="width: 250px;" readonly="readonly" autocomplete="off"/>
											<input type="hidden" name="store_id" id="store_id" value="{$order.store_id}" />
								        </div>  
								        <div class="col-xs-2">
										    <span id="err_store_name" style="color:#F00; display:none;">商铺名不能为空</span>
								        </div> 
								    </div>    
								    </td>
								</tr>
								<tr>
									<td>司机:</td>
									<td>
										<div class="form-group ">
											<div class="col-xs-2">
											<!-- <select class="input-sm" name="driver_id" id="driver_id">
												<option value="0" selected="selected">选择司机</option>
												<?php
												foreach($drivers as $v){
												?>
												<option value="{$v['user_id']}" <?php if($order['driver_id']==$v['user_id']){ echo 'selected'; };?>>{$v['nick_name']}</option>
												<?php
												}
												?>
											</select> -->
												<input name="driver_name" id="driver_name" value="{$order.driver_name}" class="form-control" placeholder="司机"  style="width: 250px;" oninput="autopush(this,'driver')"  autocomplete="off"/>
												<input type="hidden" name="driver_id" id="driver_id" value="{$order.driver_id}" />
												<ul id="driver_list" class="driver_autopush"></ul>
											</div>
											<div class="col-xs-2">
												<span id="err_driver_id" style="color:#F00; display:none;">司机不能为空</span>
											</div>
										</div>
									</td>
								</tr>
<tr>
								    <td>退货时间:</td>
								    <td>
								    <div class="form-group ">
								        <div class="col-xs-2">
								            <input name="tuihuo_time" id="tuihuo_time" value="{:date("Y-m-d")}" class="form-control" placeholder="退货时间"  style="width: 250px;" autocomplete="off"/>
								        </div>  
								        <div class="col-xs-2">
										    <span id="err_store_name" style="color:#F00; display:none;">商铺名不能为空</span>
								        </div> 
								    </div>    
								    </td>
								</tr> 
                                <?php
								if(empty(I('id'))){
								?>
								<tr>
								<td>商品列表:</td>
								<td>
								<div class="form-group">
										     <div class="col-xs-10" id="goods_td">
                                       		<table class="table table-bordered">
                                       			<thead>
                                       			<tr>
									                <td class="text-left">商品名称</td> 
									                <td class="text-left">价格</td>
													<td class="text-left">平台扣点</td>
									                <td class="text-left">数量</td>
									                <td class="text-left">金额</td>
									                <td class="text-left">买家理由（买家可见）</td>
													<td class="text-left">操作</td>
									            </tr>
									            </thead>
									            <tbody>
									           </tbody>
                                       		</table>
										     </div>
										</div>
									</td>
									</tr>
								<?php
									}else{ //查看
								?>
                                <tr>
                                    <td>商品列表:</td>
                                    <td> 
                                       <div class="form-group">
                                       		<div class="col-xs-10">
                                       		<table class="table table-bordered">
                                       			<thead>
                                       			<tr>
									            
									                <td class="text-left">商品名称</td> 
									                <td class="text-left">价格</td>								                				
									                <td class="text-left">平台扣点</td>
									                <td class="text-left">数量</td>
									                <td class="text-left">金额</td>
									                <td class="text-left">买家理由（买家可见）</td>
									            </tr>
									            </thead>
									            <tbody>
									            	<tr>
								                   
									                <td class="text-left">{$order.goods_name}（{$order.guige}）</td>
									               
									                <td class="text-left">{$order.member_goods_price}</td>
													<td class="text-left">{$order['points']}%</td> 
									                <td class="text-left">
									                <input type="hidden" name="goods_ids" rel="{$order.goods_id}" value="{$order.spec_key}">
									                <input type="text" class="input-sm" name="goods_num" value="{$order.goods_num}"></td>
									                <td class="text-left"><input type="text" class="input-sm" name="member_goods_price" value="{$order.member_goods_price}">
									                </td>
													<td class="text-left">
														<input type="text" class="input-sm" name="reason" value="{$order.reason}">
													</td>

									           		</tr>
									           </tbody>
                                       		</table>
                                       	   </div>
                                       </div>                                              
                                    </td>
                                </tr><?php }?>                                 
                                <tr>
                                    <td>订单号/备注（商家可见）:</td>
                                    <td>
                                    <div class="form-group ">
	                                    <div class="col-xs-4">
											
                                        	<!-- <textarea style="width:540px; height:150px;" name="remark" id="remark">{$order.remark|htmlspecialchars_decode}</textarea> -->
								<select class="input-sm" name="remark" id="remark">
											<option value="" selected="selected">选择备注</option>
											<?php
											foreach($remarks as $v){
											?>
											<option value="{$v}" <?php if($order['remark']==$v){ echo 'selected'; };?>>{$v}</option>
											<?php
											}
											?>
								</select>
								<div>{$order['remark']}</div>
                                        </div>
										<div class="col-xs-2">
										    <span id="err_remark" style="color:#F00; display:none;">单号/备注不能为空</span>
										</div> 
                                    </div>    
                                    </td>
                                </tr>
								<tr>
								    <td>退货类型:</td>
									<td>
										<div class="form-group ">
											<div class="col-xs-4">
												<select name="refund_type" class="input-sm">
													<option value="0" <?php if($order['refund_type']==0){ echo'selected'; };?>>退货</option>
													<option value="1" <?php if($order['refund_type']==1){ echo'selected'; };?>>退货退款</option>
												</select>
											</div>
										</div>
									</td>
								</tr>
                                
                                </tbody>
                                </table>
                        </div>
                        <input type="hidden" name="id" value="{$order.id}">
						
                        <button class="btn btn-info" type="button" onclick="checkSubmit()">
                            <i class="ace-icon fa fa-check bigger-110"></i>
                          		 保存
                        </button>&nbsp;&nbsp;&nbsp;
						
                    </form> 
                </div>
            </div>
        </div> 
    </section>
</div>
<script>
   /* 用户订单区域选择 */
$(document).ready(function(){
	$('#province').val({$order.province});
	$('#city').val({$order.city});
	$('#district').val({$order.district});
	$('#shipping_id').val({$order.shipping_id});
	const order_sn = '{$order_sn}';
	
	$('#tuihuo_time').daterangepicker({
		format:"YYYY-MM-DD",
		singleDatePicker: true,
		showDropdowns: true,
		minDate:'2021-01-01',
		maxDate:'2030-01-01',
		startDate:'{:date("Y-m-d")}',
		showWeekNumbers: true,
		timePicker: false,
		timePickerIncrement: 1,
		timePicker12Hour: false,
		opens: 'right',
		buttonClasses: ['btn btn-default'],
		applyClass: 'btn-small btn-primary',
		cancelClass: 'btn-small',
		locale : {
		    applyLabel : '确定',
		    cancelLabel : '取消',
		    fromLabel : '起始时间',
		    toLabel : '结束时间',
		    customRangeLabel : '自定义',
		    daysOfWeek : [ '日', '一', '二', '三', '四', '五', '六' ],
		    monthNames : [ '一月', '二月', '三月', '四月', '五月', '六月','七月', '八月', '九月', '十月', '十一月', '十二月' ],
		    firstDay : 1
		}
	});


	if(order_sn){
		layer.load()
		$.post('/index.php?m=admin&c=Order&a=tuihuo_keys',{order_sn:order_sn,action:'user'},function(data){
			var store_id=$(data).attr('rel');
			var user_id=$(data).attr('rev');
			var store_name=$(data).attr('alt');
			var consignee=$(data).attr('title');
			var order_id=$(data).attr('id');
			var driver_id=$(data).attr('req');
			var driver_name=$(data).attr('ref');
			var mytext=$(data).text();
			$('#consignee').val(consignee);
			$('#user_id').val(user_id);
			$('#store_name').val(store_name);
			$('#store_id').val(store_id);
			$('#order_sn').val(mytext);
			$('#order_id').val(order_id);
			$('#driver_id').val(driver_id);
			$('#driver_name').val(driver_name);
			//$('#driver_name').val(driver_name);
			$.post('/index.php?m=admin&c=Order&a=tuihuo_goods',{order_id:order_id},function(data2){
				layer.closeAll('loading');
				$('#goods_td tbody').html(data2);
			})
		})
	}

  $(document).on('click','.autopush li',function(){
	  var store_id=$(this).attr('rel');
	  var user_id=$(this).attr('rev');
	  var store_name=$(this).attr('alt');
	  var consignee=$(this).attr('title');
	  var order_id=$(this).attr('id');
	  var driver_id=$(this).attr('req');
	  var driver_name=$(this).attr('ref');
	  var mytext=$(this).text();
		  $('#consignee').val(consignee);
		  $('#user_id').val(user_id);
		  $('#store_name').val(store_name);
		  $('#store_id').val(store_id);
		  $('#order_sn').val(mytext);
		  $('#order_id').val(order_id);
		  $('#driver_id').val(driver_id);
		  $('#driver_name').val(driver_name);
		  //$('#driver_name').val(driver_name);
		  $.post('/index.php?m=admin&c=Order&a=tuihuo_goods',{order_id:order_id},function(data){
			$('#goods_td tbody').html(data);
		})
	  $(this).parent().hide();
  });
  $(document).on('click','.driver_autopush li',function(){
	  var driver_id=$(this).attr('req');
	  var driver_name=$(this).attr('ref');
	//   var mytext=$(this).text();
		  $('#driver_id').val(driver_id);
		  $('#driver_name').val(driver_name);
	  $(this).parent().hide();
  });
});
   var timer;
function autopush(e,action){

	if (timer) {
		clearTimeout(timer);
	}

	timer = setTimeout(function() {
		var key=$(e).val();
		var obj=$(e).parent();
		obj.find('.autopush').hide();
		obj.find('.driver_autopush').hide();
		if(key.length>0){
			var mayloading = layer.load(3,{shade: [0.1, '#000']});
			$.post('/index.php?m=admin&c=Order&a=tuihuo_keys',{order_sn:key,action:action},function(data){
				if(action=='user'){
					obj.find('.autopush').show();
					obj.find('#orders').html(data);layer.close(mayloading);
				}
				if(action=='driver'){
					obj.find('.driver_autopush').show();
					obj.find('#driver_list').html(data);layer.close(mayloading);
				}
			})
		}
	}, 800);
}

// 选择商品
function selectGoods(){
	var store_id=$("#store_id").val();
	if(store_id==''){
		layer.msg('请选择退货的商家！', {icon: 5});return false;
	}
    var url = "/index.php/Admin/Order/search_goodss/store_id/"+store_id;
    layer.open({
        type: 2,
        title: '选择退货商品',
        shadeClose: true,
        shade: 0.8,
        area: ['60%', '60%'],
        content: url, 
    });
}

// 选择商品返回
function call_back(table_html)
{
	$('#goods_td').append('<table class="table table-bordered">'+table_html+'</table>');
	//过滤选择重复商品
	$('input[name*="spec"]').each(function(i,o){
		if($(o).val()){
			var name='goods_id['+$(o).attr('rel')+']['+$(o).val()+'][goods_num]';
			$('input[name="'+name+'"]').parent().parent().remove();
		}
	});
	layer.closeAll('iframe');
}

function checkSubmit()
{
	$("span[id^='err_']").each(function(){
		$(this).hide();
	});
   ($.trim($('#consignee').val()) == '') && $('#err_consignee').show();
   ($.trim($('#store_name').val()) == '') && $('#err_store_name').show();
//   ($.trim($('#remark').val()) == '') && $('#err_remark').show();
   ($.trim($('#driver_id').val()) == '') && $('#err_driver_id').show();
   // if(($("input[name^='goods_id']").length ==0) && ($("input[name^='old_goods']").length == 0)){
	  //  layer.alert('订单中至少要有一个商品', {icon: 2});  // alert('少年,订单中至少要有一个商品');
	  //  return false;
   // }			
    //   var checkedGoods = $("input[name='goods_ids[]']:checked");
    // if(checkedGoods.length == 0) {
    //     layer.alert('请至少选择一个商品', {icon: 2});
    //     return false;
    // }
    
    // // 新增：只提交选中的商品
    // $("input[name='goods_ids[]']").not(':checked').each(function() {
    //     $(this).attr('disabled', 'disabled');
    // });
   if($("span[id^='err_']:visible").length > 0 ) 
      return false;							  
   $('#order-add').submit();	  
}

   // function goodsNum(e){
	// 	console.log($(this).html())
	//    let price = e * 1;
	//    $(this).parent().find('.goods_num').html(price);
   // }
</script>
</body>
</html>